package com.operate.task;

import com.alibaba.fastjson2.JSONObject;
import com.operate.bean.RiseProportion;
import com.operate.bean.VO.*;
import com.operate.config.HttpConfig;
import com.operate.service.CommonNumService;
import com.operate.service.RiseProportionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/11/6 1:29
 */
@Slf4j
@Service
public class CommonNumTask {


    @Value("${common.enable}")
    private boolean common_enable;
    @Value("${common.query.url}")
    private String common_query_url;
    @Value("${common.delete.url}")
    private String common_delete_url;
    @Value("${common.insert.url}")
    private String common_insert_url;


    @Value("${wr.month.enable}")
    private boolean wr_month_enable;
    @Value("${wr.month.query.url}")
    private String wr_month_query_url;
    @Value("${wr.month.delete.url}")
    private String wr_month_delete_url;
    @Value("${wr.month.insert.url}")
    private String wr_month_insert_url;


    @Value("${wr.day.type.enable}")
    private boolean wr_day_type_enable;
    @Value("${wr.day.type.query.url}")
    private String wr_day_type_query_url;
    @Value("${wr.day.type.delete.url}")
    private String wr_day_type_delete_url;
    @Value("${wr.day.type.insert.url}")
    private String wr_day_type_insert_url;

    @Value("${diners.num.stat.enable}")
    private boolean diners_num_stat_enable;
    @Value("${diners.num.stat.query.url}")
    private String diners_num_stat_query_url;
    @Value("${diners.num.stat.delete.url}")
    private String diners_num_stat_delete_url;
    @Value("${diners.num.stat.insert.url}")
    private String diners_num_stat_insert_url;


    @Value("${rise.proportion.enable}")
    private boolean rise_proportion_enable;
    @Value("${rise.proportion.query.url}")
    private String rise_proportion_query_url;
    @Value("${rise.proportion.delete.url}")
    private String rise_proportion_delete_url;
    @Value("${rise.proportion.insert.url}")
    private String rise_proportion_insert_url;


    @Autowired
    private CommonNumService service;
    @Autowired
    private HttpConfig httpConfig;


    @Autowired
    private RiseProportionService riseProportionService;

    @Async("myAsync")
    public void commonTask() {
        if (common_enable == false) {
            log.info("Common Task 未开启");
            return;
        }
        LocalDateTime s = LocalDateTime.now();
        //基础数据统计
        List<CommonVO> list = service.commonData();
        LocalDateTime sqlExeTime = LocalDateTime.now();
        httpConfig.clearData(common_query_url, common_delete_url);
        if (list != null && !list.isEmpty()) {
            List<Fields> insertList = new ArrayList<>();
            for (CommonVO vo : list) {
                Fields fields = new Fields(vo);
                insertList.add(fields);
            }
            Map<String, List<Fields>> insertMap = new HashMap<>();
            insertMap.put("records", insertList);
            String insertResult = httpConfig.POST(common_insert_url, JSONObject.toJSONString(insertMap));
            LocalDateTime e = LocalDateTime.now();
            long sqlTime = Duration.between(s, sqlExeTime).getSeconds();
            long seconds = Duration.between(s, e).getSeconds();
            log.info("Common Task 总耗时{}秒(SQL 耗时{}) 插入{}条记录 插入结果: {}", seconds, sqlTime, list.size(), insertResult);
        }
    }


    @Async("myAsync")
    public void wrMonthTask() {
        if (wr_month_enable == false) {
            log.info("WR Month Task 未开启");
            return;
        }
        LocalDateTime s = LocalDateTime.now();
        //等位/预约 渠道数量统计（近12个月）
        List<WRMonthNumVO> list = service.wrMonthList();
        LocalDateTime sqlExeTime = LocalDateTime.now();
        httpConfig.clearData(wr_month_query_url, wr_month_delete_url);
        if (list != null && !list.isEmpty()) {
            List<Fields> insertList = new ArrayList<>();
            for (WRMonthNumVO waitVO : list) {
                Fields fields = new Fields(waitVO);
                insertList.add(fields);
            }
            Map<String, List<Fields>> insertMap = new HashMap<>();
            insertMap.put("records", insertList);
            String insertResult = httpConfig.POST(wr_month_insert_url, JSONObject.toJSONString(insertMap));
            LocalDateTime e = LocalDateTime.now();
            long sqlTime = Duration.between(s, sqlExeTime).getSeconds();
            long seconds = Duration.between(s, e).getSeconds();
            log.info("WR Month Task 总耗时{}秒(SQL 耗时{}) 插入{}条记录 插入结果: {}", seconds, sqlTime, list.size(), insertResult);
        }
    }

    @Async("myAsync")
    public void wrDayTypeTask() {
        if (wr_day_type_enable == false) {
            log.info("Wait Day Type Task 未开启");
            return;
        }
        LocalDateTime s = LocalDateTime.now();
        //等位/预约 渠道数量统计（近30日）
        List<WRDayTypeNumVO> list = service.wrDayTypeNum();
        LocalDateTime sqlExeTime = LocalDateTime.now();
        httpConfig.clearData(wr_day_type_query_url, wr_day_type_delete_url);
        if (list != null && !list.isEmpty()) {
            List<Fields> insertList = new ArrayList<>();
            for (WRDayTypeNumVO vo : list) {
                Fields fields = new Fields(vo);
                insertList.add(fields);
            }
            Map<String, List<Fields>> insertMap = new HashMap<>();
            insertMap.put("records", insertList);
            String insertResult = httpConfig.POST(wr_day_type_insert_url, JSONObject.toJSONString(insertMap));
            LocalDateTime e = LocalDateTime.now();
            long sqlTime = Duration.between(s, sqlExeTime).getSeconds();
            long seconds = Duration.between(s, e).getSeconds();
            log.info("WR Day Type Task 总耗时{}秒(SQL 耗时{}) 插入{}条记录 插入结果: {}", seconds, sqlTime, list.size(), insertResult);

        }
    }

    @Async("myAsync")
    public void dinersNumStatTask() {
        if (diners_num_stat_enable == false) {
            log.info("Diners Num Stat Task 未开启");
            return;
        }
        LocalDateTime s = LocalDateTime.now();
        //就餐人数规模统计
        List<DinersNumStatVO> list = service.dinersNumStatList();
        LocalDateTime sqlExeTime = LocalDateTime.now();
        httpConfig.clearData(diners_num_stat_query_url, diners_num_stat_delete_url);
        if (list != null && !list.isEmpty()) {
            List<Fields> insertList = new ArrayList<>();
            for (DinersNumStatVO vo : list) {
                Fields fields = new Fields(vo);
                insertList.add(fields);
            }
            Map<String, List<Fields>> insertMap = new HashMap<>();
            insertMap.put("records", insertList);
            String insertResult = httpConfig.POST(diners_num_stat_insert_url, JSONObject.toJSONString(insertMap));
            LocalDateTime e = LocalDateTime.now();
            long sqlTime = Duration.between(s, sqlExeTime).getSeconds();
            long seconds = Duration.between(s, e).getSeconds();
            log.info("Diners Num Stat Task 总耗时{}秒(SQL 耗时{}) 插入{}条记录 插入结果: {}", seconds, sqlTime, list.size(), insertResult);
        }
    }


    @Async("myAsync")
    public void RiseProportionTask() {
        if (diners_num_stat_enable == false) {
            log.info("Rise Proportion Task  未开启");
            return;
        }
        LocalDateTime s = LocalDateTime.now();
        List<RiseProportion> list = riseProportionService.riseProportionQuery();

        LocalDateTime sqlExeTime = LocalDateTime.now();
        //清除数据
        httpConfig.clearData(rise_proportion_query_url, rise_proportion_delete_url);
        if (list != null && !list.isEmpty()) {
            List<Fields> insertList = new ArrayList<>();
            for (RiseProportion vo : list) {
                Fields fields = new Fields(vo);
                insertList.add(fields);
            }
            Map<String, List<Fields>> insertMap = new HashMap<>();
            insertMap.put("records", insertList);
            //插入数据
            String insertResult = httpConfig.POST(rise_proportion_insert_url, JSONObject.toJSONString(insertMap));
            LocalDateTime e = LocalDateTime.now();
            long sqlTime = Duration.between(s, sqlExeTime).getSeconds();
            long seconds = Duration.between(s, e).getSeconds();
            log.info("Rise Proportion Task 总耗时{}秒(SQL 耗时{}) 插入{}条记录 插入结果: {}", seconds, sqlTime, list.size(), insertResult);
        }
    }


}

